AssJS = {

    checkMainForm: function() {
        var mainForm = document.getElementById("MainForm");

        this.handleAsAmountItems();

        var uncompleted = this.getUnCompletedItem();

        if (document.activeElement.name == "resetForm") {
            return mainForm.submit();
        }
        if (document.activeElement.name == "addAss") {
            if (uncompleted.length > 0) {
                alert(document.getElementById("pleaseFillAllElements").value);
                return false;
            }
            return mainForm.submit();
        }
        if (document.activeElement.name == "asCompany"
            || document.activeElement.name == "asType"
            || document.activeElement.name == "asClass"
            || document.activeElement.name == "asName"
            || document.activeElement.name == "asArea") {
            return mainForm.submit();
        }

        if (uncompleted.length == 0) {
            return mainForm.submit();
        }

        return false;
    },

    handleAsAmountItems: function() {
        var asAmountDrp = document.getElementById("asAmount");
        var asAmountTxt = document.getElementById("asAmountTxt");

        if (document.activeElement.name == "asAmount") {
            asAmountTxt.value = asAmountDrp.value;
        }
        else {
            asAmountDrp.selectedIndex = 1;
        }

        if (this.isValid(asAmountTxt)) {
            var regu1 = /^[-]{0,1}[0-9]{1,}$/;
            if (regu1.test(asAmountTxt.value)) {
                return true;
            } else {
                alert(document.getElementById("badAsAmountInput").value);
                asAmountTxt.value = asAmountDrp.options[1].value;
                return false;
            }
        }
        return true;
    },

    addAssCheck: function() {
        var mainForm = document.getElementById("MainForm");
        var uncompleted = this.getUnCompletedItem();
        if (uncompleted.length > 0) {
            alert(document.getElementById("pleaseFillAllElements").value);
            return false;
        }
        return mainForm.submit();
    },

    isValid: function(element) {
        var elarray = element.value.split("---");
        if (elarray.length == 3 && elarray[0].length == 0 && elarray[2].length == 0) {
            return false;
        }
        return true;
    },

    getUnCompletedItem: function() {
        //if ( document.getElementById("asBirth").value == '' ) return 'asBirth';
        var asAmount = document.getElementById("asAmount");
        var asAmountTxt = document.getElementById("asAmountTxt");
        var asArea = document.getElementById("asArea");
        var asBirth = document.getElementById("asBirth");
        var asCompany = document.getElementById("asCompany");
        var asGender = document.getElementById("asGender");
        var asName = document.getElementById("asName");
        var asPeriod = document.getElementById("asPeriod");
        var asType = document.getElementById("asType");
        var asClass = document.getElementById("asClass");
        var fePeriod = document.getElementById("fePeriod");

        if (asAmountTxt.value == '') return asAmountTxt.name;
        if (asBirth.value == '') return asBirth.name;
        if (!this.isValid(asGender)) return asGender.name;
        if (!this.isValid(asArea)) return asArea.name;
        if (!this.isValid(asAmount)) return asAmount.name;
        if (!this.isValid(asCompany)) return asCompany.name;
        if (!this.isValid(asName)) return asName.name;
        if (!this.isValid(asType)) return asType.name;
        if (!this.isValid(asClass)) return asClass.name;
        if (!this.isValid(asPeriod)) return asPeriod.name;
        if (!this.isValid(fePeriod)) return fePeriod.name;

        return '';
    },

    markBtnViewSelected: function() {
        var viewBtnSelect = document.getElementById("viewBtnSelect");
        var mainForm = document.getElementById("MainForm");
        viewBtnSelect.value = document.activeElement.name;
        mainForm.submit();
        return true;
    },

    markBtnDelSelected: function() {
        var delBtnSelect = document.getElementById("delBtnSelect");
        var mainForm = document.getElementById("MainForm");
        delBtnSelect.value = document.activeElement.name;
        mainForm.submit();
        return true;
    },

    alwaysSubmit: function() {
        var mainForm = document.getElementById("MainForm");
        return mainForm.submit();
    }
}

